/************************************
 * created by Tonk (tonk@ctonk.com) *
 ************************************/

// logs.cpp

#include "definitions.h"
#include "mob.h"
#include <fstream>
#include "area.h"
#include "room.h"
#include "functions.h"
#include "stringutil.h"

int Mob::numMobs = 0;

Mob::Mob() {
	_vnum = 0;
	_whenMove = 0;
	reset();
	numMobs++;
}

/*Mob::Mob( Mob& it ) {
	_vnum = it.getVnum();
	_whenMove = it.getWhenMove();
}*/

Mob::~Mob() {
	numMobs--;
}

Mob& Mob::operator = ( Mob& it ) {
	_vnum = it.getVnum();
	_whenMove = it.getWhenMove();
	return *this;
}

void Mob::setVnum( int vnum ) {
	_vnum = vnum;
	return;
}

int Mob::getVnum() {
	return _vnum;
}

void Mob::setArea( Area* area ) {
	_area = area;
	return;
}

Area* Mob::getArea() {
	return _area;
}

void Mob::setLong( std::string long_desc ) {
	_long = long_desc;
	return;
}

std::string Mob::getLong() {
	return _long;
}

void Mob::setShort( std::string short_desc ) {
	_short = short_desc;
	return;
}

std::string Mob::getShort() {
	return _short;
}

void Mob::setWhenMove( time_t whenMove ) {
	_whenMove = whenMove;
	return;
}

time_t Mob::getWhenMove() {
	return _whenMove;
}

void Mob::move() { // private
	int dir = 0;
	Room* from = _room;
	Room* to = 0;
	std::string buf;
	int i = 0;

	if ( from->NoExits() ) {
		reset();
		return;
	}

	while ( 1 ) {
		i++;
		dir = rand() % 6;
		if ( _room->GetExit( dir ).GetThere() ) { //&& World::Instance().FindRoom( _room->GetExit( dir ).GetPoint() )->GetArea() == _area->GetNumber() ) {
			if ( ( to = World::Instance().FindRoom( _room->GetExit( dir ).GetPoint() ) ) == NULL )
				continue;
			from->Remove( this );
			buf = _short;
			if ( dir >= NORTH && dir < UP )
				buf << " has left to the " << Exit::StringName( dir ) << ".";
			else if ( dir == UP )
				buf << " has left up.";
			else
				buf << " has left down.";
			sendToRoom( from, buf );
			buf = _short;
			if ( dir >= NORTH && dir < UP )
				buf << " has arrived from the " << Exit::StringName( Exit::Inverse( dir ) );
			else if ( dir == UP )
				buf << " has arrived from below.";
			else
				buf << " has arrived from above.";
			sendToRoom( to, buf );
			to->Add( this );
			setRoom( to->GetVnum() );
			reset();
			return;
		}
		if ( i > 10 ) {
			reset();
			return;
		}
	}
}

void Mob::reset() { // private
	time_t current;
	time( &current );

	_whenMove = current + (time_t)( rand() % UPPER_ACT + LOWER_ACT );

	return;
}

void Mob::mSave( std::ofstream& save ) {
	save << "\n**MOB\n";
	// Save creature data...
	this->Save( save );

	save << "// Mob data...\n";
	save << _vnum << '\n';
	save << _short << '\n';
	save << _long << '\n';
	return;
}

void Mob::mLoad( std::ifstream& load ) {
	char tmp[10000];
	// Load creature data...
	this->Load( load );

	load >> _vnum;
	load.get();
	load.getline( tmp, 10000 );
	_short = tmp;
	load.getline( tmp, 10000 );
	_long = tmp;

	reset();

	return;
}

void Mob::update() {
	time_t current;
	time( &current );

	if ( _whenMove <= current ) {
		move();
		reset();
	}

	return;
}
